#ifndef CANVAS_H
#define CANVAS_H

#include <QWidget>
#include <QVector2D>

#define MARGEM      0.05

class Segment
{
    QVector2D m_p1;
    QVector2D m_p2;
    QColor m_color;

public:
    Segment(const QVector2D& p1 = QVector2D(),
            const QVector2D& p2 = QVector2D(),
            const QColor& color = QColor(Qt::black));

    const QVector2D& p1()const;
    QVector2D& p1();

    const QVector2D& p2()const;
    QVector2D& p2();

    const QColor& color()const;
    QColor& color();

    const Segment& operator=(const Segment& seg);
};


namespace Ui {
class Canvas;
}

class Canvas : public QWidget
{
    Q_OBJECT
    
public:
    explicit Canvas(QWidget *parent = 0);
    ~Canvas();

    void setViewPort(const QRect& vp);
    QPoint worldToSreen(const QVector2D& v);
    QVector2D screenToWorld(const QPoint& p);
    void drawLines(const QList<QVector2D> &points, const QColor &cor);
    void drawPoints(const QList<QVector2D>& points);
    void drawSegment(const Segment& s);
    void clear();
    void update();
    
private:
    Ui::Canvas *ui;
    QImage m_buffer;
    QRect viewPort;

    void paintBuffer();
    void setupBuffer();

protected:
    void resizeEvent(QResizeEvent *);
    void mousePressEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);

signals:
    void draw();
    void leftReleased(QVector2D p);
    void leftPressed(QVector2D p);
};

#endif // CANVAS_H
